<template>
    <div class="app-container">
        <el-form :model="queryParams"
                 ref="queryForm"
                 :inline="true"
                 v-show="showSearch"
                 label-width="55px">
            <el-form-item label="卡号" prop="flowCardNumber">
                <el-input
                        v-model="queryParams.flowCardNumber"
                        placeholder="请输入流量卡卡号"
                        clearable
                        size="small"
                        @keyup.enter.native="handleQuery"
                />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button
                        type="primary"
                        plain
                        icon="el-icon-plus"
                        size="mini"
                        @click="handleAdd"
                        v-hasPermi="['business:flowCard:add']"
                >新增
                </el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button
                        type="success"
                        plain
                        icon="el-icon-edit"
                        size="mini"
                        :disabled="single"
                        @click="handleUpdate"
                        v-hasPermi="['business:flowCard:edit']"
                >修改
                </el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button
                        type="danger"
                        plain
                        icon="el-icon-delete"
                        size="mini"
                        :disabled="multiple"
                        @click="handleDelete"
                        v-hasPermi="['business:flowCard:remove']"
                >删除
                </el-button>
            </el-col>
            <!--      <el-col :span="1.5">-->
            <!--        <el-button-->
            <!--          type="warning"-->
            <!--          plain-->
            <!--          icon="el-icon-download"-->
            <!--          size="mini"-->
            <!--          @click="handleExport"-->
            <!--          v-hasPermi="['business:flowCard:export']"-->
            <!--        >导出</el-button>-->
            <!--      </el-col>-->
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="cardList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center"/>
            <!--      <el-table-column label="主键" align="center" prop="flowCardId" />-->
            <el-table-column label="流量卡卡号" align="center" prop="flowCardNumber"/>
            <el-table-column label="流量总额" align="center" prop="flowTotal"/>
            <el-table-column label="剩余流量" align="center" prop="flowBalance"/>
            <el-table-column label="流量卡类型" align="center" prop="flowCardType">
                <template slot-scope="scope">
                    <dict-tag :options="dict.type.biz_flowcard_type" :value="scope.row.flowCardType"/>
                </template>
            </el-table-column>
            <el-table-column label="流量卡有效时间" align="center" prop="effectiveTime" width="180">
                <template slot-scope="scope">
                    <span>{{ parseTime(scope.row.effectiveTime, '{y}-{m}-{d}') }}</span>
                </template>
            </el-table-column>
            <el-table-column label="网关编号" align="center" prop="gatewayNumber"/>
            <!--      <el-table-column label="t_gateway_info主键，冗余" align="center" prop="gatewayInfoId" />-->
            <el-table-column label="备注" align="center" prop="remark"/>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                    <el-button
                            size="mini"
                            type="text"
                            icon="el-icon-edit"
                            @click="handleUpdate(scope.row)"
                            v-hasPermi="['business:flowCard:edit']"
                    >修改
                    </el-button>
                    <el-button
                            size="mini"
                            type="text"
                            icon="el-icon-delete"
                            @click="handleDelete(scope.row)"
                            v-hasPermi="['business:flowCard:remove']"
                    >删除
                    </el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination
                v-show="total>0"
                :total="total"
                :page.sync="queryParams.pageNum"
                :limit.sync="queryParams.pageSize"
                @pagination="getList"
        />

        <!-- 添加或修改流量卡信息对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="55px" size="small">
                <el-form-item label="类型" prop="flowCardType">
                    <el-select v-model="form.flowCardType" placeholder="请选择流量卡类型">
                        <el-option
                                v-for="item in dict.type.biz_flowcard_type"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value"/>
                    </el-select>

                </el-form-item>
                <el-form-item label="卡号" prop="flowCardNumber">
                    <el-input v-model="form.flowCardNumber" placeholder="请输入流量卡卡号"/>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button size="small" type="primary" @click="submitForm">确 定</el-button>
                <el-button size="small" @click="cancel">取 消</el-button>
            </div>
        </el-dialog>

    </div>
</template>

<script>
import {listFlowCard, getFlowCard, delFlowCard, addFlowCard, updateFlowCard} from '@/api/business/flowCard'

export default {
    name: "FlowCard",
    dicts: ['biz_flowcard_type'],
    data() {
        return {
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 流量卡信息表格数据
            cardList: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                flowCardNumber: null,
                flowTotal: null,
                flowBalance: null,
                flowCardType: null,
                effectiveTime: null,
                gatewayNumber: null,
                gatewayInfoId: null
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {
                flowCardType: [
                    { required: true, message: '请选择流量卡类型', trigger: 'blur' }
                ],
                flowCardNumber: [
                    { required: true, message: '请输入流量卡卡号', trigger: 'blur' }
                ]
            }
        }
    },
    created() {
        this.getList()
    },
    methods: {

        getList() {
            this.loading = true
            listFlowCard(this.queryParams).then(response => {
                this.cardList = response.rows
                this.total = response.total
                this.loading = false
            })
        },

        cancel() {
            this.open = false
            this.reset()
        },

        reset() {
            this.form = {
                flowCardId: null,
                flowCardNumber: null,
                flowTotal: null,
                flowBalance: null,
                flowCardType: null,
                effectiveTime: null,
                gatewayNumber: null,
                gatewayInfoId: null,
                createBy: null,
                createTime: null,
                updateBy: null,
                updateTime: null,
                remark: null
            };
            this.resetForm("form")
        },

        handleQuery() {
            this.queryParams.pageNum = 1
            this.getList()
        },

        resetQuery() {
            this.resetForm("queryForm")
            this.handleQuery()
        },

        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.flowCardId)
            this.single = selection.length !== 1
            this.multiple = !selection.length
        },

        handleAdd() {
            this.reset()
            this.open = true
            this.title = "添加流量卡信息"
        },

        handleUpdate(row) {
            this.reset()
            const flowCardId = row.flowCardId || this.ids
            getFlowCard(flowCardId).then(response => {
                this.form = response.data
                this.open = true
                this.title = "修改流量卡信息"
            })
        },

        submitForm() {
            this.$refs["form"].validate(valid => {
                if (valid) {
                    if (this.form.flowCardId != null) {
                        updateFlowCard(this.form).then(response => {
                            this.$modal.msgSuccess("修改成功")
                            this.open = false
                            this.getList()
                        })
                    } else {
                        addFlowCard(this.form).then(response => {
                            this.$modal.msgSuccess("新增成功")
                            this.open = false
                            this.getList()
                        })
                    }
                }
            })
        },

        handleDelete(row) {
            const flowCardIds = row.flowCardId || this.ids
            this.$modal.confirm('是否确认删除流量卡信息编号为"' + flowCardIds + '"的数据项？').then(function () {
                return delFlowCard(flowCardIds)
            }).then(() => {
                this.getList()
                this.$modal.msgSuccess("删除成功")
            }).catch(() => {
            })
        },

        handleExport() {
            this.download('system/card/export', {
                ...this.queryParams
            }, `card_${new Date().getTime()}.xlsx`)
        }
    }
}
</script>
